<?php defined('LFNC_PACKAGE') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Lucky_Funcs
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: sqlite.lfnc.php 259 2009-06-19 05:12:52Z molotsoft@gmail.com $
* @link $HeadURL: http://lucky-funcs.googlecode.com/svn/branches/version_1/funcs/sqlite.lfnc.php $
*/


function is_sqlite_link($db) {
	return is_resource_type($db, 'sqlite database') || is_resource_type($db, 'sqlite database (persistent)');
}

function is_sqlite_result($dbr) {
	return is_resource_type($dbr, 'sqlite result');
}


function sqlite_xopen($filename, $mode = 0666) {
	$db = sqlite_open($filename, $mode);
	if (is_sqlite_link($db)) register_shutdown_function('sqlite_close', $db);
	return $db;
}

function sqlite_xopen_msg($filename, $mode, &$error_message) { // $mode = 0666
	$db = sqlite_open($filename, $mode, $error_message);
	if (is_sqlite_link($db)) register_shutdown_function('sqlite_close', $db);
	return $db;
}


function sqlite_escape($string) {
	return sqlite_escape_string($string);
}

function sqlite_escape_deep($value) {
	return is_array($value) ? array_map('sqlite_escape_deep', $value) : sqlite_escape($value);
}

function vsprintf_sqlite_escape($format, $args) {
	return vsprintf($format, sqlite_escape_deep($args));
}

function sprintf_sqlite_escape($format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return vsprintf_sqlite_escape($format, array_shifted($args));
}


function sqlite_vqueryf($db, $query_format, $args) {
	return sqlite_query(vsprintf_sqlite_escape($query_format, $args), $db);
}

function sqlite_queryf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return sqlite_vqueryf($db, $query_format, array_shifted($args, 2));
}


function sqlite_vexecf($db, $query_format, $args) {
	return is_true($dbr = sqlite_vqueryf($db, $query_format, $args)) ? sqlite_changes($db) : $dbr;
}

function sqlite_execf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return sqlite_vexecf($db, $query_format, array_shifted($args, 2));
}


function sqlite_fetch_value($dbr, $value_col = 0) {
	$row = sqlite_fetch_array($dbr, is_wholenum($value_col) ? SQLITE_NUM : SQLITE_ASSOC);
	return isset($row[$value_col]) ? $row[$value_col] : FALSE;
}


function sqlite_fetch_rows($dbr, $result_type = SQLITE_ASSOC) {
	if (sqlite_num_rows($dbr) >= 1) sqlite_seek($dbr, 0);
	$rows = array();
	while ($row = sqlite_fetch_array($dbr, $result_type)) $rows[] = $row;
	return $rows;
}

function sqlite_fetch_values($dbr, $value_col = 0) {
	if (sqlite_num_rows($dbr) >= 1) sqlite_seek($dbr, 0);
	$values = array();
	while (!is_false($value = sqlite_fetch_value($dbr, $value_col))) $values[] = $value;
	return $values;
}


function sqlite_fetch_rows_combine($dbr, $key_col = 'id', $result_type = SQLITE_ASSOC) {
	$rows = sqlite_fetch_rows($dbr, $result_type);
	$combine = array();
	foreach (sqlite_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $rows[$i];
	return $combine;
}

function sqlite_fetch_values_combine($dbr, $key_col = 'id', $value_col = 'value') {
	$values = sqlite_fetch_values($dbr, $value_col);
	$combine = array();
	foreach (sqlite_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $values[$i];
	return $combine;
}


?>